package com.xiaoshuidi.cloud.module.contract.job;

import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.filings.config.FilingsProperties;
import com.xiaoshuidi.cloud.framework.filings.core.FilingsApi;
import com.xiaoshuidi.cloud.framework.filings.core.enums.UriEnum;
import com.xiaoshuidi.cloud.framework.filings.core.service.dto.CommonFilingsResponse;
import com.xiaoshuidi.cloud.framework.filings.core.service.dto.ContractFilingBo;
import com.xiaoshuidi.cloud.framework.filings.core.service.dto.ContractFilingResp;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.ContractRoomDO;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.filings.ContractFilingsDO;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.contract.ContractRoomMapper;
import com.xiaoshuidi.cloud.module.contract.enums.ErrorCodeConstants;
import com.xiaoshuidi.cloud.module.contract.enums.contract.ContractFilingsEnum;
import com.xiaoshuidi.cloud.module.contract.enums.contract.ContractStatusEnum;
import com.xiaoshuidi.cloud.module.contract.service.filings.ContractFilingsService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 合同备案查询
 */

@Component
@Slf4j
@RequiredArgsConstructor
public class ContractFillingsSearchJob {

    private final FilingsApi<ContractFilingResp, ContractFilingBo> searchApi;
    private final FilingsProperties filingsProperties;
    private final ContractFilingsService contractFilingsService;
    private final ContractRoomMapper contractRoomMapper;


    @XxlJob("contractFillingsSearchJob")
    @TenantIgnore
    public void execute() {
        List<ContractFilingsDO> filingsDOS = contractFilingsService.selectListSignSuccess();
        for (ContractFilingsDO filingsDO : filingsDOS) {
            ContractFilingBo bo = new ContractFilingBo();
            bo.setContractNo(filingsDO.getExternalContractNo());
            bo.setSecretKey(filingsProperties.getSecretKey());
            //查询备案情况，更新合同备案信息
            CommonFilingsResponse<ContractFilingResp> response = searchApi.execute(bo, UriEnum.CONTRACT_FILING_QUERY_URI, ContractFilingResp.class);
            if (!response.getStatus()) {
                log.error("获取合同备案信息错误: {}", response.getMsg());
                throw new ServiceException(ErrorCodeConstants.CONTRACT_FILINGS_ERROR.getCode(), ErrorCodeConstants.CONTRACT_FILINGS_ERROR.getMsg() + response.getMsg());
            }
            ContractFilingResp contractFilingResp = response.getData();
            filingsDO.setRecordCertificateNo(contractFilingResp.getRecordCertificateNo());
            filingsDO.setStatus(ContractFilingsEnum.SUCCESS.getCode());
            contractFilingsService.update(filingsDO);
            ContractRoomDO contractRoomDO = contractRoomMapper.selectById(filingsDO.getContractId());
            contractRoomDO.setRecordStatus(convertStatus2RecordStatus(contractRoomDO.getContractStatus()));
            contractRoomMapper.updateById(contractRoomDO);
        }
    }

    private int convertStatus2RecordStatus(String status) {
        if (ContractStatusEnum.ACTIVE.getCode().equals(status) || ContractStatusEnum.CHECK_IN.getCode().equals(status)) {
            return 1;
        } else if (ContractStatusEnum.NORMAL_END.getCode().equals(status)||ContractStatusEnum.ABNORMAL_END.getCode().equals(status)) {
            return 2;
        }
        return 1;
    }

}
